{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.8.5-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python38164bitbed07143d37b49c8ba24bbf4d83c1753",
   "display_name": "Python 3.8.1 64-bit"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "给定一个数组，它的第 i 个元素是一支给定股票第 i 天的价格。\n",
    "\n",
    "如果你最多只允许完成一笔交易（即买入和卖出一支股票一次），设计一个算法来计算你所能获取的最大利润。\n",
    "\n",
    "注意：你不能在买入股票前卖出股票。\n",
    "\n",
    " \n",
    "\n",
    "来源：力扣（LeetCode）\n",
    "链接：https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock\n",
    "著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "示例 1:\n",
    "```\n",
    "输入: [7,1,5,3,6,4]\n",
    "输出: 5\n",
    "解释: 在第 2 天（股票价格 = 1）的时候买入，在第 5 天（股票价格 = 6）的时候卖出，最大利润 = 6-1 = 5 。\n",
    "     注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格；同时，你不能在买入前卖出股票。\n",
    "```\n",
    "示例 2:\n",
    "```\n",
    "输入: [7,6,4,3,1]\n",
    "输出: 0\n",
    "解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。\n",
    "```\n",
    "来源：力扣（LeetCode）\n",
    "链接：https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock\n",
    "著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "思路:\n",
    "\n",
    "[7,1,5,3,6,4]\n",
    "\n",
    "暴力解法：\n",
    "```\n",
    "循环 i in [0, n]\n",
    "    循环 j in [i+1, n]\n",
    "        max_value = max( max_value,  prices[j] - prices[i])\n",
    "```\n",
    "复杂度为`O(n^2)`"
   ]
  },
  {
   "source": [
    "动态规划思路:\n",
    "\n",
    "```\n",
    "找到最小的那个值, 然后用当前的值减去最小值\n",
    "```"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def maxProfit(self, prices: List[int]) -> int:\n",
    "        if not prices:\n",
    "            return 0\n",
    "        dp = [0 ] * len(prices)\n",
    "        min_value = prices[0]\n",
    "        for i in range(1, len(prices)):\n",
    "            min_value = min(min_value, prices[i])\n",
    "            dp[i] = max(prices[i]-min_value, 0)\n",
    "        return max(dp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由于并不依赖之前的结果，所以可以优化一下空间复杂度\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    def maxProfit(self, prices: List[int]) -> int:\n",
    "        if not prices:\n",
    "            return 0\n",
    "        max_value = 0\n",
    "        min_value = prices[0]\n",
    "        for i in range(1, len(prices)):\n",
    "            min_value = min(min_value, prices[i])\n",
    "            max_value = max(prices[i]-min_value, max_value)\n",
    "        return max_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "5"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "Solution().maxProfit([7,1,5,3,6,4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "10"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "Solution().maxProfit([1,2,3,4,5,0, 10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}